from django.shortcuts import HttpResponse
from agent.chat_agent import create_agent
import json

#聊天
def chat(request):
    if request.method == "GET":
        #接受前端用户发过来的问题
        question=request.GET.get('question')
        print(f"question:{question}")
        try:
            #创建智能体
            answer=create_agent(question)
            #设置响应数据格式
            data={"code":200,"msg":"success","data":answer}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data={"code":200,"msg":"服务器错误","data":"不好意思，我不知道答案"}
            return HttpResponse(json.dumps(data))